package com.genshang.restaurant.util;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {


    public final static String Date_Pattern_yyyyMMddHHmmss = "yyyyMMddHHmmss";

    public final static String Date_Pattern_yyMMddHHmmss = "yyMMddHHmmss";

    public final static String Date_Pattern_yyyy_MM_dd_HH_mm = "yyyy-MM-dd HH:mm";

    public final static String Date_Pattern_yyyy_MM_dd = "yyyy-MM-dd";

    public final static String Date_Pattern_yyyy_M_d = "yyyy-M-d";

    public final static String Date_Pattern_yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";

    public final static String BLANK_STRING = "";

    public final static String FIRST_YEAR = "1900-01-01 00:00";

    public static String formatPureStandardDateTime(Date date) {
        return DateFormatUtils.format(date, Date_Pattern_yyyyMMddHHmmss);
    }

    public static String formatPureStandardShortDateTime(Date date) {
        return DateFormatUtils.format(date, Date_Pattern_yyMMddHHmmss);
    }

    public static void main(String[] args) {
//        System.out.println(formatPureStandardShortDateTime(new Date()));
//

        Long y = System.currentTimeMillis();
        System.out.println(y);
        System.out.println(stampToDate(y));

        Long x = y / 1000;
        System.out.println(x);
        System.out.println(x * 1000);
        System.out.println(stampToDate(x * 1000));


        System.out.println(getDatetimeStampAccurateMinute());
    }

    //将时间戳转换为时间
    public static String stampToDate(Long s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(s);
        res = simpleDateFormat.format(date);
        return res;
    }

    public static Date stringToDate(String str) {
        String strFormat = Date_Pattern_yyyy_MM_dd_HH_mm;
        if (str != null && str.length() == 10) {
            strFormat = Date_Pattern_yyyy_MM_dd;
        }

        if (str != null && str.length() == 8) {
            strFormat = Date_Pattern_yyyy_M_d;
        }

        SimpleDateFormat sdFormat = new SimpleDateFormat(strFormat);
        new Date();

        try {
            Date date = sdFormat.parse(str);
            return date;
        } catch (Exception var5) {
            var5.printStackTrace();
            return null;
        }
    }

    public static Date stringToDateFormat(String str, String strFormat) {
        SimpleDateFormat sdFormat = new SimpleDateFormat(strFormat);
        new Date();

        try {
            Date date = sdFormat.parse(str);
            return date;
        } catch (Exception var5) {
            return null;
        }
    }

    public static String dateToString(Date dt) {
        SimpleDateFormat sdFormat = new SimpleDateFormat(Date_Pattern_yyyy_MM_dd_HH_mm_ss);
        String str;
        if (dt == null) {
            return BLANK_STRING;
        } else {
            try {
                str = sdFormat.format(dt);
            } catch (Exception var4) {
                return BLANK_STRING;
            }

            if (str.equals(FIRST_YEAR)) {
                str = BLANK_STRING;
            }

            return str;
        }
    }

    public static Date addHours(Date date, int num) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(11, num);
        return cal.getTime();
    }

    public static Date addMinutes(Date date, int num) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(12, num);
        return cal.getTime();
    }

    public static Date addYear(Date date, int num) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(1, num);
        return cal.getTime();
    }

    public static long getDayNum(Date d1, Date d2) {
        long a1 = d1.getTime();
        long a2 = d2.getTime();
        long a3 = (a2 - a1) / 86400000L;
        return a3;
    }

    public static long getYearNum(Date d1, Date d2) {
        long a1 = d1.getTime();
        long a2 = d2.getTime();
        long a3 = (a2 - a1) / 86400000L;
        return a3 / 365;
    }

    public static String getBeforeDayDate() {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        date = calendar.getTime();

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 多态
        String format = df.format(date);
        return format;
    }

    public static Integer getDatetimeStampAccurateMinute() {
        return Integer.parseInt((System.currentTimeMillis() / 100000) + "");
    }

    public static Integer getDatetimeStampAccurateSecond() {
        return Integer.parseInt((System.currentTimeMillis() / 1000) + "");
    }

    /**
     * 将时间字符串转成 LocalDateTime
     *
     * @param datetimeString
     * @return
     */
    public static LocalDateTime stringToLocalDateTime(String datetimeString) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.parse(datetimeString, dateTimeFormatter);
        return localDateTime;
    }
}
